
function _require(filename: string) {
    let id = _require.resolve(filename);
    var module = zpack.modules[id];
    if (module) {
        module.load();
        return module.exports;
    } else {
        console.warn('module not found: ' + filename);
        return {};
    }
}

_require.dynamic = async function (filename: string): Promise<any> {
    let module = await zpack.resolve(filename);
    if (module == null) {
        console.warn('module not found: ' + filename);
        return {};
    }
    await module.resolve();
    module.load();
    return module.exports;
};


_require.ensure = function (filenames: string[], cb: () => void, errcb?: (err: any) => void) {
    let ps = filenames.map(filename => _require.dynamic(filename));
    Promise.all(ps).then(cb).catch(errcb || console.error);
};

_require.resolve = function (filename: string) {
    let module = zpack.modules[filename];
    if (module) {
        return module.id;
    } else {
        let file = zpack.files[filename];
        if (file && file[0]) {
            return file[0].id;
        }
        return filename;
    }
};